package com.fs.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fs.common.annotations.QueryField;
import com.fs.common.annotations.QueryType;
import com.fs.common.base.bean.query.BaseQuery;
import org.apache.commons.collections4.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;

/**
 * 查询辅助类
 *
 * @author  LiuQi 2025/9/29-21:29
 * @version V1.0
 **/
public final class QueryUtils {
    /**
     * 查询条件应用到QueryWrapper上
     * @param queryWrapper 查询构造器
     * @param q 查询条件
     * @param <Q> 查询条件类型
     */
    public static <Q extends BaseQuery> void applyQuery(QueryWrapper<?> queryWrapper, Q q) {
        queryWrapper.eq(StringUtils.isNotBlank(q.getId()), "id", q.getId())
                .in(CollectionUtils.isNotEmpty(q.getIds()), "id", q.getIds());

        // 默认解析QueryField的字段进行查询
        Class<? extends BaseQuery> aClass = q.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            QueryField queryField = field.getAnnotation(QueryField.class);
            String column = field.getName();
            QueryType type = QueryType.EQ;
            if (null != queryField) {
                if (queryField.invalid()) {
                    continue;
                }

                if (StringUtils.isNotBlank(queryField.column())) {
                    column = queryField.column();
                }

                type = queryField.value();
            }

            // 驼峰转下划线
            column = StringUtils.camelToUnderline(column);

            field.setAccessible(true);
            Object val;
            try {
                val = field.get(q);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }

            if (null == val || "".equals(val)) {
                if (type == QueryType.NULL) {
                    queryWrapper.isNull(column);
                } else if (type == QueryType.NOT_NULL) {
                    queryWrapper.isNotNull(column);
                }
                continue;
            }

            switch (type) {
                case EQ -> queryWrapper.eq(column, val);
                case NEQ -> queryWrapper.ne(column, val);
                case LE -> queryWrapper.le(column, val);
                case LT -> queryWrapper.lt(column, val);
                case GE -> queryWrapper.ge(column, val);
                case GT -> queryWrapper.gt(column, val);
                case LIKE -> queryWrapper.like(column, val);
                case IN -> {
                    Collection<?> list = (Collection<?>) val;
                    if (list.isEmpty()) {
                        continue;
                    }
                    queryWrapper.in(column, list);
                }
                case NOT_IN -> {
                    Collection<?> list = (Collection<?>) val;
                    if (list.isEmpty()) {
                        continue;
                    }
                    queryWrapper.notIn(column, list);
                }
                case DYNA_NULL -> {
                    // 根据值判断是否可为空
                    Boolean b = (Boolean) val;
                    if (b) {
                        queryWrapper.isNull(column);
                    } else {
                        queryWrapper.isNotNull(column);
                    }
                }
            }
        }
    }
}
